﻿using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace DotnetSpider.Lawyee.Src
{
	//public class TimedBackgroundService : BackgroundService
	//{
	//	private Timer _timer;

	//	public TimedBackgroundService()
	//	{
	//	}

	//	protected override async Task ExecuteAsync(CancellationToken stoppingToken)
	//	{
	//		_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(60));
	//		await Task.CompletedTask;
	//	}

	//	private void DoWork(object state)
	//	{
	//		LawSpider.isSchedulerEmpty = true;
	//	}

	//	public override void Dispose()
	//	{
	//		base.Dispose();
	//		_timer?.Dispose();
	//	}
	//}


	public class TimedBackgroundService : BackgroundService
	{
		private CancellationTokenSource _CancelSource;

		public async Task StartAsync()
		{
			_CancelSource = new CancellationTokenSource();
			await base.StartAsync(_CancelSource.Token);
		}

		public async Task CancelAsync()
		{
			await base.StopAsync(_CancelSource.Token);
		}

		protected override async Task ExecuteAsync(CancellationToken stoppingToken)
		{
			while (!stoppingToken.IsCancellationRequested)
			{
				await Task.Delay(30000, stoppingToken).ContinueWith(tsk =>
				{
					LawSpider.isSchedulerEmpty = true;
				});
			}
		}
	}


}
